#include "base/common.h"
#include <direct.h>

bool InitializeNet() {
#ifdef WIN32
  WSADATA wsaData;
  if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
    return false;
  }
#endif
  return true;
}

void UninitializeNet() {
#ifdef WIN32
  WSACleanup();
#endif
}

void DeleteDirectory(const char *path, bool include_self/* = true*/) {
#ifdef WIN32
  WIN32_FIND_DATA wfd;
  ::ZeroMemory(&wfd, sizeof(wfd));

  std::string subpath = std::string(path) + "\\*";
  HANDLE hFind = ::FindFirstFile(subpath.c_str(), &wfd);

  if (hFind == INVALID_HANDLE_VALUE) {
    return;
  }

  do {
    std::string filename = wfd.cFileName;

    if (filename == "." || filename == "..") {
      continue;
    }

    std::string filepath = std::string(path) + "\\" + filename;

    if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
      DeleteDirectory(filepath.c_str(), true);
    } else {
      DeleteFile(filepath.c_str());
    }
  } while (::FindNextFile(hFind, &wfd));

  if (include_self) {
    RemoveDirectory(path);
  }

  ::FindClose(hFind);
#else
#endif
}

void MakeDirectory(const char *path) {
#ifdef WIN32
  CreateDirectory(path, NULL);
#else
#endif
}
